package auxiliadormatricula.componentes;

import java.util.Map;
import java.util.TreeMap;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import auxiliadormatricula.logica.NoDisciplina;
import auxiliadormatricula.logica.StatusDisciplina;
import auxiliadormatricula.persistencia.DBAdapter;

public class TelaVisualizacao extends Activity implements android.view.View.OnClickListener {
	private LinearLayout fluxograma; 
	Map<String, Button> botoes = new TreeMap<String, Button>();
	Map<String, NoDisciplina> cadeiras = new TreeMap<String, NoDisciplina>();
	private DBAdapter dataSource ;
	private int idAtual = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.visualizar_disc);
		fluxograma = (LinearLayout) findViewById(R.id.LLVisual);
		fluxograma.setBackgroundColor(0x00000000);
		dataSource = new DBAdapter(this);
		preencherFluxograma();
		//atualizarBotoes();
	}
	
	private void preencherFluxograma() {
		NoDisciplina[][] blocos = {
		{NoDisciplina.PROGRAMACAO1, NoDisciplina.LABPROG1, NoDisciplina.LPT, NoDisciplina.CALCULO1, 
	     NoDisciplina.IC, NoDisciplina.VETORIAL},
		{NoDisciplina.PROGRAMACAO2, NoDisciplina.LABPROG2, NoDisciplina.GRAFOS, NoDisciplina.CALCULO2, 
	     NoDisciplina.DISCRETA, NoDisciplina.METODOLOGIA, NoDisciplina.FISICACLASSICA},
	    {NoDisciplina.EDA, NoDisciplina.LEDA, NoDisciplina.LINEAR, NoDisciplina.PROB, NoDisciplina.TEORIA,
	     NoDisciplina.FISICAMODERNA, NoDisciplina.GI},
	    {NoDisciplina.OAC, NoDisciplina.LOAC, NoDisciplina.PLP, NoDisciplina.METODOS, NoDisciplina.LOGICA,
	     NoDisciplina.SI1, NoDisciplina.ENGENHARIA},
	    {NoDisciplina.ATALGORITMOS, NoDisciplina.REDES, NoDisciplina.COMPILADORES, NoDisciplina.BDADOS1,
	     NoDisciplina.SI2, NoDisciplina.LES, NoDisciplina.INFOSOC},
	    {NoDisciplina.SOPERACIONAIS, NoDisciplina.IRC, NoDisciplina.LIRC, NoDisciplina.IARTIFICIAL, 
	     NoDisciplina.BDADOS2, NoDisciplina.REDESNEURAIS, NoDisciplina.METODOSFORMAIS, NoDisciplina.DIREITO},
	    {NoDisciplina.MSN, NoDisciplina.ADSD, NoDisciplina.RELACOESHUMANAS, NoDisciplina.OPT4, NoDisciplina.OPT5,
	     NoDisciplina.PROJETO1, NoDisciplina.OPT6},
	    {NoDisciplina.OPT7, NoDisciplina.OPT8, NoDisciplina.OPT9, NoDisciplina.OPT10, NoDisciplina.OPT11,
	     NoDisciplina.PROJETO2}};
		try{
			for(NoDisciplina[] bloco : blocos) {
				fluxograma.addView(criarLayout(bloco));
			}
		} catch (Exception e) {
		Log.w("!!", "message"+e.getMessage());
		}
		atualizarBotoes();
    }

	private LinearLayout criarLayout(NoDisciplina[] nds) {
    	LinearLayout periodo = new LinearLayout(this);
    	periodo.setPadding(5, 280, 0, 0);
    	periodo.setOrientation(LinearLayout.VERTICAL);
    	periodo.setBackgroundColor(0x00000000);
    	dataSource.open();
		for (NoDisciplina nd : nds) {
			idAtual++;
			NoDisciplina ndPersistido = dataSource.getDiscByNomeEStatusAtual(nd.getNome()); // recupera o no persistido do BD
			Button b = criarBotao(ndPersistido.getNome());
			b.setId(botoes.size());
			b.setOnClickListener(this);
			botoes.put(ndPersistido.getNome(), b);
			cadeiras.put(ndPersistido.getNome(), ndPersistido);
			periodo.addView(b);
		}
		dataSource.close();
		
		return periodo;
	}

	private Button criarBotao(String nome) {
		Button b = new Button(this);
		b.setText(nome);
		b.setTextColor(0xffffffff);
		registerForContextMenu(b);
		return b;
	}

	private void coloreBotao(Button b, NoDisciplina nd) {
		if(nd.getStatusTexto().equals(StatusDisciplina.NAOCURSADA.getTexto())){
			b.setBackgroundResource(R.drawable.botao_cinza);			
		}if(nd.getStatusTexto().equals(StatusDisciplina.CURSANDO.getTexto())){
			b.setBackgroundResource(R.drawable.botao_laranja);
		}if(nd.getStatusTexto().equals(StatusDisciplina.CURSADA.getTexto())){
			b.setBackgroundResource(R.drawable.botao_verde);
		}
		if(!nd.disponivel()) {
			b.setClickable(false);
			b.setBackgroundResource(R.drawable.botao_vermelho);
		} else {
			b.setClickable(true);

		}
	}
	
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
    	if(cadeiras.get(((Button)findViewById(v.getId())).getText().toString()).disponivel()) {
	    	super.onCreateContextMenu(menu, v, menuInfo);
			menu.setHeaderTitle("Estado");
			for(StatusDisciplina sd : StatusDisciplina.values()) {
				menu.add(0, v.getId(), 0, sd.getTexto());				
			}
    	}
	}
    @Override  
    public boolean onContextItemSelected(MenuItem item) {
    	Button b = (Button)findViewById(item.getItemId());
    	NoDisciplina c = cadeiras.get(b.getText());
    	for(StatusDisciplina sd : StatusDisciplina.values()) {
    		if(b.getText().equals(sd.getTexto())) {
    			c.setStatus(sd);
    		}
    	}
    	atualizarBotoes();
    	return true;
	}
    private void atualizarBotoes() {
    	for(String k : cadeiras.keySet()) {
    		coloreBotao(botoes.get(k), cadeiras.get(k));
    	}
    }
    
    @Override
    public void onClick(View v) {
    	Button b = (Button) findViewById(v.getId());
    	NoDisciplina nd = cadeiras.get(b.getText());
    	if(nd.disponivel()) {
    		switch(nd.getStatus()) {
    		case CURSANDO:
    			nd.setStatus(StatusDisciplina.CURSADA);
    			break;
    		case CURSADA:
    			nd.setStatus(StatusDisciplina.NAOCURSADA);
    			break;
    		case NAOCURSADA:
    			nd.setStatus(StatusDisciplina.CURSANDO);
    			break;
    		};
    		nd.atualizar(this);
    		atualizarBotoes();
    		dataSource.open();
    		dataSource.atualizarDisc(nd);
    		dataSource.close();
    	}
    }
    
	
	@Override
	protected void onRestart() {
		preencherFluxograma();
	}
}
